package com.test.cordova.plugins;

import android.content.ClipData;
import android.content.ClipboardManager;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 复制粘贴
 */
public class Pasteboard extends CordovaPlugin {

    private final static String TAG = "Pasteboard";
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("record".equals(action)) {
            this.record(args, callbackContext);
            return true;
        }else if ("paste".equals(action)) {
            this.paste(args, callbackContext);
            return true;
        }else if ("clear".equals(action)) {
            this.clear(args, callbackContext);
            return true;
        }else {
            LOG.i(TAG, "复制粘贴参数有误：");
        }
        return false;
    }

    private void paste(JSONArray args, CallbackContext callbackContext) {
        try {
            //剪切板管理工具类
            ClipboardManager clipboardManager = (ClipboardManager) cordova.getActivity().getSystemService(CLIPBOARD_SERVICE);
            ClipData clipData = clipboardManager.getPrimaryClip();
            //获取到内容
            ClipData.Item item = clipData.getItemAt(0);
            String text = item.getText().toString();
            callbackContext.success(text);
            LOG.i(TAG, "粘贴：" + text);
        }catch (Exception e) {
            e.printStackTrace();
            try {
                LOG.i(TAG, "粘贴失败");
                callbackContext.success(new JSONArray("[{state:-1, error:操作失败}]"));
            } catch (JSONException e1) {}
        }
    }

    private void record(JSONArray args, CallbackContext callbackContext) {

        try {
            //剪切板管理工具类
            ClipboardManager clipboardManager = (ClipboardManager) cordova.getActivity().getSystemService(CLIPBOARD_SERVICE);
            //创建一个新的文本clip对象
            ClipData clipData = ClipData.newPlainText("Simple test", args.get(0).toString());
            //把clip对象放在剪贴板中
            clipboardManager.setPrimaryClip(clipData);

            LOG.i(TAG, "复制成功：" + args.get(0).toString());
            callbackContext.success(new JSONArray("[{state:0, error:\"\"}]"));
        }catch (Exception e) {
            e.printStackTrace();
            try {
                LOG.i(TAG, "复制失败");
                callbackContext.success(new JSONArray("[{state:-1, error:操作失败}]"));
            } catch (JSONException e1) {}
        }
    }

    /**
     * 清空剪贴板
     * @param args
     * @param callbackContext
     */
    private void clear(JSONArray args, CallbackContext callbackContext) {
        try {
            //剪切板管理工具类
            ClipboardManager clipboardManager = (ClipboardManager) cordova.getActivity().getSystemService(CLIPBOARD_SERVICE);
            //创建一个新的文本clip对象
            ClipData clipData = ClipData.newPlainText("Simple test", "");
            //把clip对象放在剪贴板中
            clipboardManager.setPrimaryClip(clipData);

            LOG.i(TAG, "粘贴板清空");
            callbackContext.success(new JSONArray("[{state:0, error:\"\"}]"));
        }catch (Exception e) {
            e.printStackTrace();
            try {
                LOG.i(TAG, "粘贴板清空失败");
                callbackContext.success(new JSONArray("[{state:-1, error:操作失败}]"));
            } catch (JSONException e1) {}
        }
    }

}
